<?php
/**
* 退款
*/
class Refund extends CS_Controller
{
	
	public function _init()
	{
		$this->load->helper(array('common'));
		$this->load->model('order_base_model', 'order_base');
		$this->load->model('order_edit_model', 'order_edit');
		$this->load->model('tourism_order_refund_model', 'tourism_order_refund');
		$this->load->model('tourism_order_barter_model', 'tourism_order_barter');
		$this->load->model('express_record_model', 'express_record');
		$this->load->model('line_order_refund_model', 'line_order_refund');
	}

	/**
	 * 景区退款列表
	 */
	public function sceneryRefundList()
	{
		$page = 20;
		$total = $this->order_edit->refundOrderTotal(array('uid' => $this->uid));
		if ( $total > 0 )
		{
			$data['refundList'] = $this->order_edit->queryRefundOrder(array('uid' => $this->uid), 0, $page);
		}

		$this->data['more'] = $total > $page ? true : false;
		$this->load->view('home/refund/sceneryRefundList', $this->data);
	}

	/**
	 * ajax查询景区退单
	 */
	public function ajaxSceneryRefundList()
	{
		$page = $this->input->post('page');
		$num = 20;
		$total = $this->order_edit->refundOrderTotal(array('uid' => $this->uid));
		$data['refundList'] = $this->order_edit->queryRefundOrder(array('uid' => $this->uid), $num * $page, $num);

		echo json_encode(
				array(
					'status' => ($total >  ($page + 1) * $num) ? true : false,
					'html' => $this->load->view('home/refund/ajaxSceneryRefundList', $data, true)
				)
			);
		exit;
	}

	/**
	 * 景区退款详情
	 */
	public function sceneryRefundDetail($editId)
	{
		$edit = $this->order_edit->findById($editId);
		if ( $edit->num_rows() == 0 )
		{
			$this->alertJumpPre('订单不存在');
		}
		$edit = $edit->row();

		$orderId = ( $edit->flag == 2 ) ? $edit->n_order_id : $edit->order_id ;

		$this->data['orderInfo'] = $this->order_base->findById($orderId, $this->uid)->row();

		$this->data['editDetail'] = $edit;

		$this->load->view('home/refund/sceneryRefundDetail', $this->data);
	}

	/**
	 * 商品退款列表
	 */
	public function tourismRefundList()
	{
		$count = 20;
		$total = $this->tourism_order_refund->refundPageTotal(array('uid' => $this->uid));
		if ( $total > 0 ) {
			$this->data['refundList'] = $this->tourism_order_refund->refundList(array('uid' => $this->uid), $count);
		}

		$this->data['more'] = $total > $count ? true : false;
		$this->load->view('home/refund/tourismRefundList', $this->data);
	}

	/**
	 * 商品退款ajax
	 */
	public function ajaxTourismRefund()
	{
		$page = $this->input->post('page');
		$count = 20;
		$total = $this->tourism_order_refund->refundPageTotal(array('uid' => $this->uid));
		$data['refundList'] = $this->tourism_order_refund->refundList(array('uid' => $this->uid), $count, $count * $page);
		echo json_encode(
				array(
					'status' => ($total >  ($page + 1) * $count) ? true : false,
					'html' => $this->load->view('home/refund/ajaxTourismRefund', $data, true),
					'possess' => ($total == 0) ? false : true
				)
			);
		exit;
	}

	/**
	 * 商品换货
	 */
	public function ajaxTourismBarter()
	{
		$page = $this->input->post('page');
		$count = 20;
		$total = $this->tourism_order_barter->tourismBartchTotal(array('uid' => $this->uid));
		$data['refundList'] = $this->tourism_order_barter->tourismBartchList(array('uid' => $this->uid), $count, $count * $page);
		echo json_encode(
				array(
					'status' => ($total >  ($page + 1) * $count) ? true : false,
					'html' => $this->load->view('home/refund/ajaxTourismBartch', $data, true),
					'possess' => ($total == 0) ? false : true
				)
			);
		exit;
	}

	/**
	 * 商品退款详情
	 */
	public function tourismRefundDetail($id)
	{
		$result = $this->tourism_order_refund->findByOrderProductId($id);
        if ($result->num_rows() <= 0) {
            $this->alertJumpPre('该订单没有退款信息');
        }
		$this->data['refund'] = $result->row();
		$this->load->view('home/refund/tourismRefundDetail', $this->data);
	}

	/**
	 * 商品换货详情
	 */
	public function tourismBarterDetail($id)
	{
		$result = $this->tourism_order_barter->findProductInfo($id);
        if ($result->num_rows() <= 0) {
            $this->alertJumpPre('该订单没有换货信息');
        }
		$this->data['refund'] = $result->row();
		$this->load->view('home/refund/tourismBarterDetail', $this->data);
	}

	/**
	 * 商品物流
	 */
	public function logistics()
	{
		$company = $this->input->get('company');
		$number  = $this->input->get('number');
        $logisticsInfo = $this->express_record->findByNu($company, $number);
        if ( $logisticsInfo->num_rows() <= 0 )
        {
        	$this->alertJumpPre('该订单没有换货信息');
        }
		$this->data['logisticsInfo'] = $logisticsInfo->row();
		$this->load->view('home/refund/logistics', $this->data);
	}

	/**
	 * 线路退款列表
	 */
	public function lineRefundList()
	{
		$page = 20;
		$total = $this->line_order_refund->total(array('uid' => $this->uid));
		if ( $total > 0 )
		{
			$this->data['refundList'] = $this->line_order_refund->refundList(array('uid' => $this->uid), 0, $page);
		}

		$this->data['more'] = $total > $page ? true : false;
		$this->load->view('home/refund/lineRefundList', $this->data);
	}

	/**
	 * ajax查询线 路退单退单
	 */
	public function ajaxLineRefundList()
	{
		$page = $this->input->post('page');
		$num = 20;
		$total = $this->line_order_refund->total(array('uid' => $this->uid));
		$data['refundList'] = $this->line_order_refund->refundList(array('uid' => $this->uid), $num * $page, $num);
		echo json_encode(
				array(
					'status' => ($total >  ($page + 1) * $num) ? true : false,
					'html' => $this->load->view('home/refund/ajaxLineRefundList', $data, true)
				)
			);
		exit;
	}

	/**
	 * 线路退款详情
	 */
	public function lineRefundDetail($refundId)
	{
		$edit = $this->line_order_refund->findByOrderInfo($refundId, $this->uid);
		if ( $edit->num_rows() == 0 )
		{
			$this->alertJumpPre('订单不存在');
		}
		$this->data['orderInfo'] = $edit->row();
		$this->load->view('home/refund/lineRefundDetail', $this->data);
	}
}
